package com.ouyeel.wlxer.model.imp;

import android.app.Activity;
import android.content.Intent;

import com.ouyeel.wlxer.activity.LoginActivity;
import com.ouyeel.wlxer.activity.MainActivity;
import com.ouyeel.wlxer.activity.ManualClockingActivity;
import com.ouyeel.wlxer.api.RetrofitClicent;
import com.ouyeel.wlxer.base.BaseActivity;
import com.ouyeel.wlxer.base.BaseObserver;
import com.ouyeel.wlxer.bean.BaseRsponseOut;
import com.ouyeel.wlxer.bean.GoWrokIn;
import com.ouyeel.wlxer.bean.ManualOut;
import com.ouyeel.wlxer.bean.UploadqrcodeOut;
import com.ouyeel.wlxer.model.MainModel;
import com.ouyeel.wlxer.util.Config;
import com.ouyeel.wlxer.util.SharePrefUtil;
import com.ouyeel.wlxer.util.StringUtils;

import io.reactivex.Observable;
import okhttp3.RequestBody;

/**
 * Created by rankaifeng on 2017/10/26.
 */

public class MainModelImp implements MainModel {
    private RetrofitClicent retrofitClicent = RetrofitClicent.getInstanse();
    private static final int REQUEST_SUESS_STATUS = 1;//请求成功
    private static final int REQUEST_ERROR_STATUS = 2;//请求失败 该司机已经存在上班

    /**
     * 上班打卡
     *
     * @param activity
     * @param goWrokIn
     */
    @Override
    public void uploadqrcode(final Activity activity, GoWrokIn goWrokIn) {
        Observable<UploadqrcodeOut> uploadqrcode =
                retrofitClicent.getRetrofit(UploadqrcodeOut.class).uploadqrcode(goWrokIn);
        retrofitClicent.toSubscribe(uploadqrcode,
                new BaseObserver<UploadqrcodeOut>(activity) {
                    @Override
                    public void success(UploadqrcodeOut out) {
                        if (out.getFlag() == REQUEST_SUESS_STATUS) {
                            if (activity instanceof MainActivity) {
//                                ((MainActivity) activity).scanInfoSuccess(out);
                            } else if (activity instanceof ManualClockingActivity) {
                                ((ManualClockingActivity) activity).goWorkSuccess(out);
                            }
                        } else if (out.getFlag() == REQUEST_ERROR_STATUS) {
//                            ((MainActivity) activity).scanInfoError();
                        } else {
                            goWorkError(out.getFlag(), activity, out.getMsg());
                        }
                    }
                });
    }

    /**
     * 退出登陆
     *
     * @param activity
     * @param body
     */
    @Override
    public void outlogin(final MainActivity activity, RequestBody body) {
        Observable<BaseRsponseOut> outlogin =
                retrofitClicent.getRetrofit(BaseRsponseOut.class).outlogin(body);
        retrofitClicent.toSubscribe(outlogin,
                new BaseObserver<BaseRsponseOut>(activity) {
                    @Override
                    public void success(BaseRsponseOut baseRsponse) {
                        if (baseRsponse.getFlag() == REQUEST_SUESS_STATUS) {
//                            activity.outLoginSuccess();
                        } else {
                            if (StringUtils.isEmpty(baseRsponse.getMsg())) {
                                activity.showTost("退出登陆失败！");
                            } else {
                                activity.showTost(baseRsponse.getMsg());
                            }
                        }
                    }
                });
    }

    private void goWorkError(int flag, Activity activity, String msg) {
        if (flag == Config.TOKEN_ERROR) {
            Intent mIntent = new Intent(activity, LoginActivity.class);
            activity.startActivity(mIntent);
            ((BaseActivity) activity).showTost(" token失效！");
        } else if (flag == Config.GOWORK_CONFLICT) {
            boolean isWork =
                    SharePrefUtil
                            .getBoolean(activity, "isWork", false);
            boolean isW = !isWork;
            SharePrefUtil.saveBoolean(activity, "isWork", isW);
            ((BaseActivity) activity).showTost(msg);
        } else {
            ((BaseActivity) activity).showTost("打卡失败！");
        }
    }

    /**
     * 下班打卡
     *
     * @param activity
     * @param workIn
     */
    @Override
    public void clockOut(final MainActivity activity, GoWrokIn workIn) {
        Observable<BaseRsponseOut> clockOutObservable = retrofitClicent
                .getRetrofit(BaseRsponseOut.class).clockOut(workIn);
        retrofitClicent.toSubscribe(clockOutObservable, new BaseObserver<BaseRsponseOut>(activity) {
            @Override
            public void success(BaseRsponseOut obj) {
                if (obj.getFlag() == REQUEST_SUESS_STATUS) {
//                    activity.scanGoOffWorkSuccess();
                } else {
                    goWorkError(obj.getFlag(), activity, obj.getMsg());
                }
            }
        });
    }

    @Override
    public void getCarNumbers(final ManualClockingActivity activity, RequestBody body) {
        Observable<ManualOut> carNumber = retrofitClicent.getRetrofit(ManualOut.class)
                .getCarNumber(body);
        retrofitClicent.toSubscribe(carNumber, new BaseObserver<ManualOut>(activity) {
            @Override
            public void success(ManualOut obj) {
                if (obj.getFlag() == REQUEST_SUESS_STATUS) {
                    activity.getCarNumbersSuccess(obj);
                } else {
                    activity.showTost(obj.getMsg());
                }
            }
        });
    }
}
